//
//  HMEmoticonTool.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
//最多显示的列数
let HMEmoticonsMaxCol = 7
//最多显示的行数
let HMEmoticonsMaxRow = 3
//最多显示表情个数
let HMEmoticonsMaxCount = HMEmoticonsMaxCol*HMEmoticonsMaxRow - 1

class HMEmoticonTool: NSObject {
    
    //全局访问点
    static let sharedTools: HMEmoticonTool = HMEmoticonTool()
    //读取bundle文件
    lazy var emoticonBundle:NSBundle = {
        //路径
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)!
        return NSBundle(path:path)!
    }()
    
    //默认表情 一维数组
    lazy var defaultEmoticons:[HMEmoticonModel] = {
        return self.getEmoticonsWithPath("default")
    }()
    
    //emoji表情 一维数组
    lazy var emojiEmoticons:[HMEmoticonModel] = {
        return self.getEmoticonsWithPath("emoji")
    }()
    
    //浪小花表情 一维数组
    lazy var lxhEmoticons:[HMEmoticonModel] = {
        return self.getEmoticonsWithPath("lxh")
    }()
    
    //三维数组
    lazy var allEmoticons: [[[HMEmoticonModel]]] = {
        return[
            self.getEmoticonGroupsWithEmoticons(self.defaultEmoticons),
            self.getEmoticonGroupsWithEmoticons(self.emojiEmoticons),
            self.getEmoticonGroupsWithEmoticons(self.lxhEmoticons)
        ]
    }()
    

    //一位数组转二维数组
    func getEmoticonGroupsWithEmoticons(emoticons:[HMEmoticonModel]) -> [[HMEmoticonModel]]{
        //获取最多显示的页数
        let pageCount = (emoticons.count + HMEmoticonsMaxCount - 1)/HMEmoticonsMaxCount
        //创建一个临时的可变二维数组
        var tempArr: [[HMEmoticonModel]] = [[HMEmoticonModel]]()
        
        //遍历页数
        for i in 0..<pageCount {
            
            let location = i * HMEmoticonsMaxCount
            
            var length = HMEmoticonsMaxCount
            //由于默认表情为108个 遍历的最后一组[100,20] 而我们是[100,8]
            if location + length  > emoticons.count {
            
                length = emoticons.count - location
            
            }
            
            //范围
            let range = NSRange(location: location, length: length)
            //数组截取
            let array = (emoticons as NSArray).subarrayWithRange(range) as! [HMEmoticonModel]
            
            tempArr.append(array)
     
        }
         return tempArr
    }
 
    
    //读取plist文件 得到一维数组
    func getEmoticonsWithPath(path: String) -> [HMEmoticonModel]{
        
        let file = emoticonBundle.pathForResource("\(path)/info.plist", ofType: nil)!
        
        let array = NSArray(contentsOfFile: file)!
        //创建一个临时可变数组
        var tempArr:[HMEmoticonModel] = [HMEmoticonModel]()
        
        //遍历数组 字典转模型
        for dic in array{
            //创建一个模型
            let model = HMEmoticonModel(dict: dic as! [String: AnyObject])
            //设置路径
            model.path = "\(path)/"
            //添加模型到数组
            tempArr.append(model)
            
        }
        return tempArr
    }
    
    //遍历当前默认表情和浪下花
    func searchEmoticonInDefaultEmoticonsOrLxhEmoticons(chs: String) -> HMEmoticonModel?{
    //遍历
        for emoticonModel in defaultEmoticons{
        //判断外界传入的和当前的是否相等
            if emoticonModel.chs == chs{
            return emoticonModel
            }
        }
        for emoticonModel in lxhEmoticons{
            //判断外界传入的和当前的是否相等
            if emoticonModel.chs == chs{
                return emoticonModel
            }
        }
       //代表没有可选表情
        return nil
    }
    
}
